//===- MIROps.cpp - Implement the Moore MIR operations --------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implement the Moore MIR ops.
//
//===----------------------------------------------------------------------===//

#include "circt/Dialect/Moore/MIROps.h"
#include "mlir/IR/Builders.h"

using namespace circt;
using namespace circt::moore;

//===----------------------------------------------------------------------===//
// Type Inference
//===----------------------------------------------------------------------===//

LogicalResult ConcatOp::inferReturnTypes(MLIRContext *context,
                                         std::optional<Location> loc,
                                         ValueRange operands,
                                         DictionaryAttr attrs,
                                         mlir::RegionRange regions,
                                         SmallVectorImpl<Type> &results) {
  Domain domain = Domain::TwoValued;
  unsigned size = 0;
  for (auto operand : operands) {
    auto type = operand.getType().cast<UnpackedType>().getSimpleBitVector();
    if (type.domain == Domain::FourValued)
      domain = Domain::FourValued;
    size += type.size;
  }
  results.push_back(
      SimpleBitVectorType(domain, Sign::Unsigned, size).getType(context));
  return success();
}

//===----------------------------------------------------------------------===//
// Custom LValue parser and printer
//===----------------------------------------------------------------------===//

static ParseResult parseLValueType(OpAsmParser &p, Type &lValueType) {
  Type type;
  if (p.parseType(type))
    return p.emitError(p.getCurrentLocation(), "expected type");
  lValueType = LValueType::get(type);
  return success();
}

static void printLValueType(OpAsmPrinter &p, Operation *, Type lValueType) {
  p.printType(lValueType.cast<LValueType>().getNestedType());
}

//===----------------------------------------------------------------------===//
// TableGen generated logic.
//===----------------------------------------------------------------------===//

// Provide the autogenerated implementation guts for the Op classes.
#define GET_OP_CLASSES
#include "circt/Dialect/Moore/Moore.cpp.inc"
#include "circt/Dialect/Moore/MooreEnums.cpp.inc"
